#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

void connectIp()
{
	system("/usr/bin/adb connect 192.168.2.111:5555");
}

void dealOper(const char *oper, const char *device, std::string &result)
{
	std::string str = "/usr/bin/adb ";

	if (device == "") {

	}
	else {
        if (device[0] == '5'
		|| device[0] == '6') {
            str.append("-s emulator-55");
            str.append(device);
            str.append(" ");
        }
		// if (0 == strcmp(device, "54")) {
		// 	str.append("-s emulator-5554 ");
  //       }
  //       else if (0 == strcmp(device, "56")) {
  //       	str.append("-s emulator-5556 ");
  //       }
        else if (0 == strcmp(device, "ip")) {
        	connectIp();
        	str.append("-s 192.168.2.111:5555 ");
        }
	}

	str.append(oper);
	result = str;
    printf("device = %s\n", result.c_str());
	// system(str.c_str());
}

void deal3Args(int argc, char *argv[])
{
	std::string oper = "";
	if (argc < 3) {
        dealOper(argv[1], "", oper);
    }
    else {
        dealOper(argv[1], argv[2], oper);
    }
    system(oper.c_str());
}

void dealpullpush(std::string &oper, char *src, char *dst)
{
	oper.append(" ");
	oper.append(src);
	oper.append(" ");
	oper.append(dst);
}

void deal5Args(int argc, char *argv[])
{
	std::string oper = "";
	if (argc == 4) {
		dealOper(argv[1], "", oper);
		dealpullpush(oper, argv[2], argv[3]);
	    system(oper.c_str());
	}
	else if (5 == argc) {
		dealOper(argv[1], argv[2], oper);
		dealpullpush(oper, argv[3], argv[4]);
	    system(oper.c_str());
	}
	else {

	}
}

int main(int argc, char *argv[])
{
    for (int i = 1; i < argc; i++)
        printf("Argument %d is %s.\n", i, argv[i]);

    if (argc < 2) {
        return 0;
    }

    if (0 == strcmp(argv[1], "shell")
    	|| 0 == strcmp(argv[1], "remount")) {
		deal3Args(argc, argv);
    }
    else if (0 == strcmp(argv[1], "dcnct")) {
    	system("/usr/bin/adb disconnect");
    }
    else if (0 == strcmp(argv[1], "dev")) {
    	system("/usr/bin/adb devices");
    }
    else if (0 == strcmp(argv[1], "cnct")) {
    	system("/usr/bin/adb connect 192.168.2.111:5555");
    }
    else if (0 == strcmp(argv[1], "push")) {
    	deal5Args(argc, argv);
    }
    else if (0 == strcmp(argv[1], "pull")) {
    	deal5Args(argc, argv);
    }
    else {

    }

    return 0;
}
